<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增书籍</title>
    <style>
        :root {
            --sidebar-width: 200px;
            --primary-color: #2c3e50;
        }
        .form-container {
            background: white;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.1);
            max-width: 800px;
            margin: 0 auto;
        }

        .form-header {
            border-bottom: 2px solid var(--primary-color);
            margin-bottom: 25px;
            padding-bottom: 10px;
        }

        .form-header h3 {
            color: var(--primary-color);
            margin: 0;
        }

        .two-column-form {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 20px 30px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        label {
            font-weight: 500;
            color: #495057;
        }

        .form-input {
            padding: 10px;
            border: 1px solid #ced4da;
            border-radius: 4px;
            transition: border-color 0.3s;
        }

        .form-input:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(44, 62, 80, 0.25);
        }

        .form-actions {
            grid-column: 1 / -1;
            text-align: center;
            margin-top: 25px;
        }

        .btn-submit {
            background-color: #5cb85c;
            color: white;
            padding: 12px 40px;
            border-radius: 8px;
            border: 1px solid rgba(0,0,0,0.1);
            font-weight: 600;
            letter-spacing: 0.5px;
            transition: all 0.3s;
            box-shadow: 0 2px 6px rgba(0,0,0,0.1);
        }

        .btn-submit:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(44, 62, 80, 0.2);
        }

        .btn-cancel {
            background: linear-gradient(145deg, #6c757d, #5a6268);
            color: white;
            padding: 12px 40px;
            border-radius: 8px;
            border: 1px solid rgba(0,0,0,0.1);
            font-weight: 600;
            letter-spacing: 0.5px;
            transition: all 0.3s;
        }

        .btn-cancel:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(108, 117, 125, 0.2);
        }

        @media (max-width: 768px) {
            .btn-submit, .btn-cancel {
                width: 100%;
                padding: 14px 20px;
            }

            .two-column-form {
                grid-template-columns: 1fr;
            }

            .form-actions {
                display: flex;
                flex-direction: column;
                gap: 10px;
            }

            .btn-submit, .btn-cancel {
                width: 100%;
                margin: 0;
            }
        }
    </style>
</head>
<body>
<!-- 新增/编辑页面 -->
<div class="form-container">
    <div class="form-header">
        <h3>图书信息管理</h3>
    </div>
    <form class="two-column-form" id="bookForm">
        <div class="form-group">
            <label>ISBN：</label>
            <input type="text" class="form-input" name="isbn" required>
        </div>
        <div class="form-group">
            <label>书名：</label>
            <input type="text" class="form-input" name="title" required>
        </div>
        <div class="form-group">
            <label>作者：</label>
            <input type="text" class="form-input" name="author" required>
        </div>
        <div class="form-group">
            <label>出版社：</label>
            <input type="text" class="form-input" name="publisher">
        </div>
        <div class="form-group">
            <label>出版日期：</label>
            <input type="date" class="form-input" name="publishDate">
        </div>
        <div class="form-group">
            <label>库存数量：</label>
            <input type="number" class="form-input" min="0" name="stock">
        </div>
        <div class="form-actions">
            <button type="submit" class="btn-submit" id="btnOk">保存修改</button>
            <button type="button" class="btn-cancel" id="btnCancel">返回列表</button>
        </div>
    </form>
</div>

<script src="/static/myajax.js"></script>
<script>
    document.getElementById('bookForm').addEventListener('submit', function(e) {
        e.preventDefault();

        const formData = {
            isbn: this.isbn.value,
            title: this.title.value,
            author: this.author.value,
            publisher: this.publisher.value,
            publish_date: this.publishDate.value || "2000-01-01",
            stock: parseInt(this.stock.value) || 0
        };
        console.log(formData)
        ajaxRequest( {
            method: 'POST',
            url:"/jwt/bookAdd",
            data: formData
        }).then(data => {
            if(data.status == 0) {
                if (confirm('继续添加，选ok，返回列表页面选cancel')){
                    document.getElementById('bookForm').reset();
                }else{
                    window.parent.document.querySelector('iframe[name="contentFrame"]').src = '/static/book_list.html';
                }

            }else{
                alert(data.status + '：' + data.msg)
            }
        }).catch(error => alert('Error:'+ error.msg));


    });

    // 返回按钮事件
    document.getElementById('btnCancel').addEventListener('click', function() {
        window.parent.document.querySelector('iframe[name="contentFrame"]').src = '/static/book_list.html';
    });
</script>
</body>
</html>



